package com.hb.game.pk.ui.activity.mine;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.event.ChangeHeadEvent;
import com.hb.game.pk.event.RefreshUserInfoEvent;
import com.hb.game.pk.utils.LogUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

public class MineMsgActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.wv_web)
    WebView wvWeb;

    @Override
    protected int getLayoutId() {
        return R.layout.acitivity_mine_msg;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MineMsgActivity.this.finish();
            }
        });
        getKJJL();
    }

    private void getKJJL() {
        WebSettings webSetting = wvWeb.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setAllowFileAccess(true);
        wvWeb.addJavascriptInterface(this, "android");
        WebChromeClient client = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }

            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                return super.onConsoleMessage(consoleMessage);
            }
        };
        wvWeb.setWebChromeClient(client);
//            Map<String, String> map = new HashMap<String, String>();
//            map.put("token", APP.Token);
//            map.put("userid", APP.uid);
//            webView.loadUrl("https://www.baidu.com", map);
        String url = "file:///android_asset/msg/myinfo.html";
        wvWeb.loadUrl(url);
        wvWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
//                view.loadUrl("function:start('" + APP.uid + "','" + APP.Token + "')");
                view.evaluateJavascript("start('" + APP.uid + "','" + APP.Token + "')", new ValueCallback<String>() {
                    //                view.evaluateJavascript("function:callJs()", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        LogUtils.d("value____   " + value);
                    }
                });
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webCheckBack()) return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private boolean webCheckBack() {
        if (wvWeb.canGoBack()) {
            wvWeb.goBack();
        } else {
            this.finish();
        }
        return true;
    }

    @JavascriptInterface
    public void doJsRequest() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webCheckBack();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().post(new RefreshUserInfoEvent());
    }
}
